package net.web.inst.struts2.normal;

import java.util.List;

import javax.annotation.Resource;

import net.esj.basic.exception.MelonException;
import net.esj.basic.service.DefaultService;
import net.esj.basic.utils.CollectionUtils;
import net.esj.basic.utils.StringUtils;
import net.melon.sso.pojo.Member;
import net.melon.sso.service.SSOService;
import net.melon.uploader.ErrorInfo;
import net.melon.uploader.Uploader;
import net.melon.uploader.exception.UploadRuntimeException;
import net.melon.uploader.pojo.FileUpload;
import net.melon.uploader.service.FileNameRule;
import net.melon.uploader.service.PathRule;
import net.melon.uploader.service.impl.DateSplitPathRule;
import net.melon.uploader.service.impl.RandomFileNameRule;
import net.web.dto.OrderDto;
import net.web.dto.ReviewDto;
import net.web.inst.service.NormalService;
import net.web.inst.struts2.WebAction;

public class UserAction extends WebAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3891180352000280302L;
	
	private String un;
	
	@Resource
	private SSOService ssoService;
	
	@Resource
	private NormalService normalService;
	
	@Resource
	private DefaultService defaultService;
	
	private String colour;
	
	private Long regionId; 
	
	private ReviewDto reviewDto;
	
	private PathRule pathRule = new DateSplitPathRule();
	
	private FileNameRule fileNameRule = new RandomFileNameRule();
	
	Uploader uploader = new Uploader(pathRule, fileNameRule, "review");
	
	public void saveOrder(){
		try {
			if(getMember()!=null && StringUtils.hasText(id) && StringUtils.hasText(colour) && regionId!=null && regionId!=0){
				String orderId = normalService.saveOrder(id,colour,regionId,getMember());
				map.put("suc", suc);
				map.put("orderId",orderId);
			}else{
				map.put("suc", fail);
			}
		} catch (MelonException e){
			map.put("msg",e.getMessage());
			map.put("suc", fail);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("msg","系统错误");
			map.put("suc", fail);
		}
		writeJson(map);
	}
	
	public void deleteOrder(){
		try {
			if(getMember()!=null && StringUtils.hasText(id)){
				normalService.deleteOrder(id);
				map.put("suc", suc);
			}else{
				map.put("suc", fail);
			}
		} catch (MelonException e) {
			map.put("suc", fail);
		} catch (Exception e) {
			map.put("suc", fail);
		}
		writeJson(map);
	}
	
	public void getMyOrder(){
		try {
			if(getMember()==null){
				map.put("suc", fail);
			}else{
				List<OrderDto> list = normalService.getMyOrder(getMember());
				map.put("suc", suc);
				map.put("list", list);
			}
		} catch (Exception e) {
			map.put("suc", fail);
		}
		writeJson(map);
	}
	
	
	public void getOrderDetail(){
		try {
			if(getMember()==null || !StringUtils.hasText(id)){
				map.put("suc", fail);
			}else{
				OrderDto order = normalService.getOrderDetail(getMember(),id);
				map.put("suc", suc);
				map.put("order", order);
			}
		} catch (Exception e) {
			map.put("suc", fail);
		}
		writeJson(map);
	}
	
	public void review(){
		try {
			if(getMember()==null || reviewDto==null){
				map.put("suc", fail);
			}else{
				normalService.review(reviewDto,getMember());
				map.put("suc", suc);
			}
		} catch (MelonException e) {
			map.put("suc", fail);
		} catch (Exception e) {
			map.put("suc", fail);
		}
		writeJson(map);
	}
	
	public void upload() throws Exception{
		try{
			uploader.setAllowFiles(new String[]{".jpg",".png",".gif",".jpeg",".bmp"});
			List<FileUpload> list = uploader.upload(getRequest());
			if(!CollectionUtils.isEmpty(list)){
				FileUpload fu = list.get(0);
				writeString("{'state':'"+ErrorInfo.SUCCESS+"','url':'"+fu.getUrl()+"','original':'"+fu.getOriginal()+"'}");
			}else{
				writeString("{state:'"+ErrorInfo.UNKNOW+"'}");
			}
		}catch(UploadRuntimeException e){
			writeString("{state:'"+e.getMessage()+"'}");
		}
	}
	
	public String getUn() {
		return un;
	}

	public void setUn(String un) {
		this.un = un;
	}

	public String getColour() {
		return colour;
	}

	public void setColour(String colour) {
		this.colour = colour;
	}

	public Long getRegionId() {
		return regionId;
	}

	public void setRegionId(Long regionId) {
		this.regionId = regionId;
	}
	
	private Member getMember(){
		Member member = getSessionUser(Member.class);
		return member;
	}

	public ReviewDto getReviewDto() {
		return reviewDto;
	}

	public void setReviewDto(ReviewDto reviewDto) {
		this.reviewDto = reviewDto;
	}
}
